using System;
using Science.Mathematics;
using L=Science.Physics.GeneralPhysics;

namespace Serway.Chapter08
{
	/// <summary>
	/// Example02: Ball in Free Fall
	/// A ball of mass m is dropped from a height h above 
	/// the ground, as shown in Figure 8.6.
	/// (A) Neglecting air resistance, determine the speed of 
	/// the ball when it is at a height y above the ground.
	/// v_f = \sqrt{2g(h-y)}
	/// (B) Determine the speed of the ball at y if at the
	/// instant of release it already has an initial upward
	/// speed v_i at the initial altitude h.
	/// v_f = \sqrt{v_i^2+2g(h-y)}
	/// </summary>
	public class Example02
	{
		public Example02()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
		private double m = 5.0, h = 10.0, y = 2.0, vel = 10.0;
		public void Compute()
		{
			L.Scalar.FunctionOfPosition func = new L.Scalar.FunctionOfPosition(Grav);
			L.PotentialEnergy pot = new L.PotentialEnergy();
            pot.ScalarFunctionOfPosition = func;
			L.Position ri = new L.Position();
			ri.Y = h;
			L.Position rf = new L.Position();
			rf.Y = y;			
			L.Work W = new L.Work();
            W.J = -pot.Difference(ri,rf);
			L.Mass mass = new L.Mass();
			mass.kg = m;
			L.Velocity vi = new L.Velocity();
			vi.Y = vel;
			L.KineticEnergy ki = new L.KineticEnergy(mass,vi);
			L.KineticEnergy kf = new L.KineticEnergy();
			kf.VariableQ = true;
		
			L.FundamentalLaw.WorkEnergyTheorem(ki,W,kf);
		
			L.Velocity vf = new L.Velocity(mass,kf);
			result += Convert.ToString(vf.mPERs)+"  ";
			double ans = Math.Sqrt(vel*vel+2.0*L.Constant.AccelerationOfGravity*(h-y));
			result += Convert.ToString(ans);

		}

        private L.Scalar Grav(L.Position x)
        {
            L.Scalar s = new L.Scalar();
			s.Magnitude = m*L.Constant.AccelerationOfGravity*x.Y;
            return s;
		}
	}
}
